package com.ruoyi.system.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.ProjectPicture;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectFileMapper;
import com.ruoyi.system.domain.ProjectFile;
import com.ruoyi.system.service.IProjectFileService;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 项目附件Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-06
 */
@Service
public class ProjectFileServiceImpl implements IProjectFileService {
    @Resource
    private ProjectFileMapper projectFileMapper;

    /**
     * 查询项目附件
     *
     * @param pfilId 项目附件ID
     * @return 项目附件
     */
    @Override
    public ProjectFile selectProjectFileById(Long pfilId) {
        return projectFileMapper.selectProjectFileById(pfilId);
    }

    public ProjectPicture selectProjectPictureById(Long picId) {
        return projectFileMapper.selectProjectPictureById(picId);
    }

    /**
     * 查询项目附件列表
     *
     * @param projectFile 项目附件
     * @return 项目附件
     */
    @Override
    public List<ProjectFile> selectProjectFileList(ProjectFile projectFile) {

        return projectFileMapper.selectProjectFileList(projectFile);
    }

    /**
     * 查询项目附件列表
     *
     * @param projectFile 项目图片
     * @return 项目附件
     */
    @Override
    public List<ProjectPicture> selectProjectPictureList(ProjectPicture projectFile) {
//        String profile = RuoYiConfig.getProfile();
        List<ProjectPicture> projectPictures = projectFileMapper.selectProjectPictureList(projectFile);
//        for (int i = 0; i < projectPictures.size(); i++) {
//            String getFileUrl = projectPictures.get(i).getPicUrl();
//            String replace = getFileUrl.replace("profile", profile);
//            projectPictures.get(i).setPicUrl(replace);
//        }
        return projectPictures;
    }

    /**
     * 新增项目附件
     *
     * @param projectFile 项目附件
     * @return 结果
     */
    @Override
    public int insertProjectFile(ProjectFile projectFile) {
        return projectFileMapper.insertProjectFile(projectFile);
    }

    /**
     * 修改项目附件
     *
     * @param projectFile 项目附件
     * @return 结果
     */
    @Override
    public int updateProjectFile(ProjectFile projectFile) {
        return projectFileMapper.updateProjectFile(projectFile);
    }

    /**
     * 批量删除项目附件
     *
     * @param pfilIds 需要删除的项目附件ID
     * @return 结果
     */
    @Override
    public int deleteProjectFileByIds(Long[] pfilIds) {
        String profile = RuoYiConfig.getProfile();
        int a = 0;
        for (int i = 0; i < pfilIds.length; i++) {
            ProjectFile projectFile1 = projectFileMapper.selectProjectFileById(pfilIds[i]);
            String getFileUrl = projectFile1.getFileUrl();
            String replace = getFileUrl.replace("profile", profile);
            projectFile1.setFileUrl(replace);
            if (new File(projectFile1.getFileUrl()).delete()) {
                a += projectFileMapper.deleteProjectFileById(pfilIds[i]);
            } else {
                a += 0;
            }
        }
        return a;
    }

    /**
     * 批量删除项目图片
     *
     * @param picIds 需要删除的项目图片ID
     * @return 结果
     */
    @Override
    public int updateProjectPicture(Long picIds) {
        String profile = RuoYiConfig.getProfile();
        int a = 0;
        ProjectPicture projectPicture = projectFileMapper.selectProjectPictureById(picIds);
        String getFileUrl = projectPicture.getPicUrl();
        String replace = getFileUrl.replace("profile", profile);
        projectPicture.setPicUrl(replace);
        if (new File(projectPicture.getPicUrl()).delete()) {
            a += projectFileMapper.updateProjectPicture(picIds);
        } else {
            a += projectFileMapper.updateProjectPicture(picIds);
        }
        return a;
    }

    /**
     * 删除项目附件信息
     *
     * @param pfilId 项目附件ID
     * @return 结果
     */
    @Override
    public int deleteProjectFileById(Long pfilId) {
        ProjectFile projectFile1 = projectFileMapper.selectProjectFileById(pfilId);
        if (new File(projectFile1.getFileUrl()).delete()) {
            return projectFileMapper.deleteProjectFileById(pfilId);
        } else {
            return 0;
        }

    }

    @Override
    public int insertProjectPicture(ProjectPicture projectFile) {

        return projectFileMapper.insertProjectPicture(projectFile);
    }
}
